﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using Bazookabird.Common;
using Bazookabird.Common.Plugin;
using Bazookabird.Wpf.Model;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace Bazookabird.Wpf.Queries.QueryHandlers.SimpleSprintViewer
{
    [Export(typeof(IQueryHandler))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class SimpleSprintViewerViewModel : PanelViewModelBase, IQueryHandler, IRefresh
    {
        public SimpleSprintViewerViewModel()
        {
            StartDate = new DateTime(2011, 11, 10);
            EndDate = new DateTime(2011, 11, 16);
        }

        public override string Description { get { return "Show where you are in a sprint."; } }
        public string Name { get { return "Sprint overview"; } }

        protected override void OnActivate()
        {
            base.OnActivate();
            DisplayName = Name;
        }

        private WorkItemStore _workItemStore;
        public WorkItemStore WorkItemStore
        {
            get { return _workItemStore; }
            set
            {
                _workItemStore = value;
                NotifyOfPropertyChange(() => WorkItemStore);
            }
        }

        private QueryDefinition _queryDefinition;
        public QueryDefinition QueryDefinition
        {
            get { return _queryDefinition; }
            set
            {
                _queryDefinition = value;
                NotifyOfPropertyChange(() => QueryDefinition);

                Refresh();
            }
        }

        private List<WorkItemNode> _workItemCollection;
        public List<WorkItemNode> WorkItemCollection
        {
            get { return _workItemCollection; }
            set
            {
                _workItemCollection = value;
                NotifyOfPropertyChange(() => WorkItemCollection);
            }
        }

        private WorkItemTimelineCollection _workItemTimelineCollection;
        public WorkItemTimelineCollection WorkItemTimelineCollection
        {
            get { return _workItemTimelineCollection; }
            set
            {
                _workItemTimelineCollection = value;
                MaxScale = _workItemCollection != null ? _workItemTimelineCollection.GetMaxScale() : null;
                NotifyOfPropertyChange(() => WorkItemTimelineCollection);
            }
        }

        private double? _maxScale;

        public double? MaxScale
        {
            get { return _maxScale; }
            set
            {
                _maxScale = value;
                NotifyOfPropertyChange(() => MaxScale);
            }
        }

        private DateTime _startDate;
        public DateTime StartDate
        {
            get { return _startDate; }
            set 
            {
                _startDate = value;
                NotifyOfPropertyChange(() => StartDate);
            }
        }

        private DateTime _endDate;
        public DateTime EndDate
        {
            get { return _endDate; }
            set
            {
                _endDate = value;
                NotifyOfPropertyChange(() => EndDate);
            }
        }

        private DateTime _lastUpdated;
        public DateTime LastUpdated
        {
            get { return _lastUpdated; }
            set
            {
                _lastUpdated = value;
                NotifyOfPropertyChange(() => LastUpdated);
            }
        }

        public new void Refresh()
        {
            WorkItemCollection = QueryHelper.FetchItems(WorkItemStore, QueryDefinition);
            WorkItemTimelineCollection = new WorkItemTimelineCollection(WorkItemCollection, StartDate, EndDate);
            LastUpdated = DateTime.Now;
            base.Refresh();
        }
    }
}
